<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/quest.jsp"%>
<%!

  String myReply(Integer uid, Input input) {
    Message msg = new Message();
    Integer itemCount = 20;
    Integer hospital = input.getInt("hospital", -1); // 医院id
    // 此since_id和max_id为评论ID
    String sinceID = input.getDesc("since_id","0"); // 下拉刷新
    String maxID = input.getDesc("max_id","0"); // 上拉加载
	Integer groupId = input.getInt("group", -1); // 分组id, 默认全部分类问题
	String extra = "";
	if (groupId != -1) {
		extra = " and q.group_id = " + groupId;
	}
    String having = "";
    String order = "desc";
    if (!sinceID.equals("0")) {
      having = " having cq.c_id > " + sinceID;
      order = "asc";
    } else if (!maxID.equals("0")) {
      having = " having cq.c_id <" + maxID;
    }
    org.hibernate.StatelessSession statelessSession = DBTools.db("basedb").getSf().openStatelessSession();
    String sql= String.format("select distinct cq.q_id , cq.c_id, cq.uid as c_uid, cq.q_title, cq.q_imgs, cq.q_uid , cq.q_content, cq.q_create_time, cq.q_aid, cq.q_group , cq.c_imgs, cq.c_content, cq.c_create_time, cq.c_parent_id, cq.c_at_uid from (select q.id as q_id, c.id as c_id, c.uid as uid, q.title as q_title,case when q.imgs is null then '[]' else q.imgs end as q_imgs, q.uid as q_uid, q.content as q_content, q.create_time as q_create_time, q.aid as q_aid, q.group_id as q_group,case when c.imgs is null then '[]' else c.imgs end as c_imgs, c.content as c_content, c.create_time as c_create_time, c.parent_id as c_parent_id, c.at_uid as c_at_uid from quest as q, comment as c where q.id=c.src_id and q.status=1 and c.status=1 %s and q.hospital=%s and c.uid=%s order by c.id %s) as cq group by cq.q_id %s order by cq.c_id %s", new Object[]{extra, hospital, uid, order, having, order});
	List<Map> list = new ArrayList<Map>();
	try {
		list = statelessSession.createSQLQuery(sql).setFirstResult(0).setMaxResults(itemCount).setResultTransformer(org.hibernate.transform.Transformers.ALIAS_TO_ENTITY_MAP).list();
		if (statelessSession != null)
			statelessSession.close();
	} catch (Exception e) {
		e.printStackTrace();
	}
    if (isEmpty(list))
       msg.setErrcodeAndErrmsg(30041, "没有更多了");
    List<Map> questList = new ArrayList<>();
    List<Map> commentList = new ArrayList<>();
    for (Map map:list) {
        Map questMap = new HashMap();
        questMap.put("uid", map.get("q_uid"));
        questMap.put("aid", map.get("q_aid"));
        questMap.put("content", map.get("q_content"));
        questMap.put("create_time", map.get("q_create_time"));
        questMap.put("group", map.get("q_group"));
        questMap.put("id", map.get("q_id"));
        questMap.put("imgs", map.get("q_imgs"));
        questMap.put("title", map.get("q_title"));
        questList.add(questMap);
        Map commentMap = new HashMap();
        commentMap.put("uid", map.get("c_uid"));
        commentMap.put("content", map.get("c_content"));
        commentMap.put("create_time", map.get("c_create_time"));
        commentMap.put("id", map.get("c_id"));
        commentMap.put("imgs", map.get("c_imgs"));
        commentMap.put("parent_id", map.get("c_parent_id"));
        commentMap.put("at_uid", map.get("c_at_uid"));
        commentList.add(commentMap);
    }
    completeQuestUserInfo(questList, hospital, uid);
    completeCommentUserInfo(commentList, hospital, uid);
    List<Map> resultList = new ArrayList<>();
    for (int i = 0; i < list.size(); i++) {
      Map result = new HashMap();
      result.put("comment", commentList.get(i));
      result.put("quest", questList.get(i));
      resultList.add(result);
    }
    msg.put("data", resultList);
    return msg.toJson();
  }
%>
<%
	Tools.encryptPrint(out, myReply(token.getUid(), input));
%>
